package com.tripreset.sdk

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import com.tencent.smtt.sdk.WebSettings
import com.tencent.smtt.sdk.WebView
import com.tencent.smtt.sdk.WebViewClient

class MainActivity2 : AppCompatActivity() {

    companion object {
        fun start(context: Context, url: String) {
            val intent = Intent(context, MainActivity2::class.java)
            intent.putExtra("url", url)
            ContextCompat.startActivity(context, intent, null)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main2)

        val webView = findViewById<WebView>(R.id.webview)

        val webSetting: WebSettings = webView.settings
        webSetting.javaScriptEnabled = true
        webSetting.javaScriptCanOpenWindowsAutomatically = true
        webSetting.allowFileAccess = true
        webSetting.layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS
        webSetting.setSupportZoom(true)
        webSetting.useWideViewPort = true
        webSetting.setSupportMultipleWindows(true)
        webSetting.setAppCacheEnabled(true)
        webSetting.domStorageEnabled = true
        webSetting.setGeolocationEnabled(true)
        webSetting.cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK


        webView.webViewClient = object : WebViewClient() {

            /*override fun shouldInterceptRequest(webView: WebView?, url: String?): WebResourceResponse {
                //做广告拦截，ADFIlterTool 为广告拦截工具类
                return if (!hasAd(url?.toString()?:"")) {
                    super.shouldInterceptRequest(webView, url)
                } else {
                    WebResourceResponse(null, null, null)
                }
            }*/

            override fun onPageFinished(view: WebView, url: String) {
                webView.evaluateJavascript(
                    "javascript:(" +
                            "    function() {" +
                            "            var len = document.getElementsByClassName('Advertisement').length; " +
                            "            for(var i = 0; i < len; i ++){" +
                            "                document.getElementsByClassName('Advertisement')[i].style.display = 'none'" +
                            "            }" +
                            "        }" +
                            ")()"
                ) {
                    print(it)
                }
            }

        }

        val url = intent.getStringExtra("url")
        webView.loadUrl(url)
    }
}